package com.funcourse.demo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.funcourse.demo.model.CompeteRoom;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisTemplateConfig {
  @Bean
  @Primary
  RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory rcf) {

    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(rcf);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new JsonRedisSerializer());

    return template;
  }
  static class JsonRedisSerializer implements RedisSerializer<Object> {

    private final ObjectMapper om;

    public JsonRedisSerializer() {
      this.om = new ObjectMapper();
    }

    @Override
    public byte[] serialize(Object t) throws SerializationException {
      try {
        return om.writeValueAsBytes(t);
      } catch (JsonProcessingException e) {
        throw new SerializationException(e.getMessage(), e);
      }
    }

    @Override
    public CompeteRoom deserialize(byte[] bytes) throws SerializationException {

      if(bytes == null){
        return null;
      }

      try {
        return om.readValue(bytes, CompeteRoom.class);
      } catch (Exception e) {
        throw new SerializationException(e.getMessage(), e);
      }
    }
  }

}
